package curves.gui;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import curves.Surface;

public class StepChooserDialog extends JDialog {

    private JSlider slider;
    private JButton button;
    private JPanel panel;
    private JLabel label;
    private Surface surface;
    private Window window;

    public StepChooserDialog(Window aFrame, int steps, Surface surface) {
        super(aFrame, true);
        this.surface = surface;
        this.window = aFrame;
        panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
        label = new JLabel("Set desired precision:");
        label.setAlignmentX(Component.CENTER_ALIGNMENT);
        panel.add(label);

        slider = new JSlider(1, 10);
        slider.setMajorTickSpacing(3);
        slider.setMinorTickSpacing(1);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);
        slider.setSnapToTicks(true);
        slider.addChangeListener(new SliderChangeListener());
        slider.setValue(steps);
        panel.add(slider);

        button = new JButton("Ok");
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        button.addActionListener(new OkButtonListener());
        panel.add(button);

        getContentPane().add(panel);

        pack();
        setResizable(false);

    }

    class OkButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            StepChooserDialog.this.setVisible(false);
        }
    }

    class SliderChangeListener implements ChangeListener {

        public void stateChanged(ChangeEvent e) {
            StepChooserDialog.this.setTitle("Steps: " + slider.getValue());
            surface.setSteps(slider.getValue());
            window.getGraphicEngine().repaint();
        }
    }

    public int getSteps() {
        return slider.getValue();
    }
}
